package com.miyake.dsp.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;


public class DspTextField extends DspSettingElement {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5155366263459452007L;
	JTextField text;
	public DspTextField(String title) {
		this.setTitle(title);
		text = new JTextField();
		text.setPreferredSize(new Dimension(50, WIDGET_HIGHT));
		this.add(text);
		
		text.addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				if (arg0.getKeyChar() == KeyEvent.VK_ENTER && !text.getText().isEmpty()) {
					fireChange(text.getText());
				}
			}
			
		});
		
		text.addFocusListener(new FocusListener() {

			@Override
			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void focusLost(FocusEvent arg0) {
				if (!text.getText().isEmpty()) {
//					fireChange(text.getText());
				}
			}
			
		});
	}

	@Override
	protected Component getInputComponent() {
		return text;
	}

	public void setValue(String value) {
		text.setText(value);
	}
	
	public void setEditable(boolean b) {
		this.text.setEditable(b);
	}

	public double getValue() {
		return Double.valueOf(text.getText());
	}
}
